package fileController

import (
	"CMETO/conf"
	"CMETO/models"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
)

// 通用文件上传接口·
type UploadController struct{}

// @Summary 文件上传
// @Description 通用文件上传接口
// @Tags 资源管理
// @Accept json
// @Produce json
// @Param file body models.CommonFile true "文件"
// @Success 200 {object} models.UploadSuccessResult "上传成功"
// @fail 400 {object} models.UploadSuccessResult "上传失败"
// @Router /upload [post]
// @Security JWT
func (f UploadController) FileUpload(ctx *gin.Context) {
	fileHeader, err1 := ctx.FormFile("file")
	if err1 != nil {
		ctx.JSON(http.StatusOK, models.UploadSuccessResult{
			Msg:    "上传失败",
			Code:   "400",
			Result: "",
		})
		return
	}
	AccessKey, SerectKey, Bucket, _ := conf.Conf{}.LoadConfig()
	key := "cmeto/image/IMG_" + strconv.FormatInt(time.Now().UnixMilli(), 10) + fileHeader.Filename
	putPlicy := storage.PutPolicy{
		Scope:   Bucket,
		SaveKey: key,
	}
	mac := qbox.NewMac(AccessKey, SerectKey)
	upToken := putPlicy.UploadToken(mac)
	cfg := storage.Config{
		Zone:          &storage.ZoneHuanan,
		UseCdnDomains: false,
		UseHTTPS:      false,
	}
	putExtra := storage.PutExtra{
		Params: map[string]string{
			"x:name": fileHeader.Filename,
		},
	}
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}
	file, _ := fileHeader.Open()
	defer file.Close()
	err := formUploader.PutWithoutKey(ctx, &ret, upToken, file, fileHeader.Size, &putExtra)
	if err != nil {
		ctx.JSON(http.StatusOK, models.UploadSuccessResult{
			Msg:    "上传失败",
			Code:   "400",
			Result: "",
		})
		return
	}
	print(key)
	ctx.JSON(http.StatusOK, models.UploadSuccessResult{
		Msg:    "上传成功",
		Code:   "200",
		Result: key,
	})
}

// @Summary 视频上传
// @Description 视频上传接口
// @Tags 资源管理
// @Accept json
// @Produce json
// @Param file body models.CommonFile true "文件"
// @Success 200 {object} models.UploadSuccessResult "上传成功"
// @fail 400 {object} models.UploadSuccessResult "上传失败"
// @Router /upload/video [post]
// @Security JWT
func (f UploadController) VideoUpload(ctx *gin.Context) {
	fileHeader, err1 := ctx.FormFile("file")
	if err1 != nil {
		ctx.JSON(http.StatusOK, models.UploadSuccessResult{
			Msg:    "上传失败",
			Code:   "400",
			Result: "",
		})
		return
	}
	AccessKey, SerectKey, Bucket, _ := conf.Conf{}.LoadConfig()
	key := "cmeto/video/Video_" + strconv.FormatInt(time.Now().UnixMilli(), 10) + fileHeader.Filename
	putPlicy := storage.PutPolicy{
		Scope:   Bucket,
		SaveKey: key,
	}
	mac := qbox.NewMac(AccessKey, SerectKey)
	upToken := putPlicy.UploadToken(mac)
	cfg := storage.Config{
		Zone:          &storage.ZoneHuanan,
		UseCdnDomains: false,
		UseHTTPS:      false,
	}
	putExtra := storage.RputV2Extra{}
	uploader := storage.NewResumeUploaderV2(&cfg)
	ret := storage.PutRet{}
	file, _ := fileHeader.Open()
	defer file.Close()
	err := uploader.PutWithoutKey(ctx, &ret, upToken, file, fileHeader.Size, &putExtra)
	if err != nil {
		ctx.JSON(http.StatusOK, models.UploadSuccessResult{
			Msg:    "上传失败",
			Code:   "400",
			Result: "",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.UploadSuccessResult{
		Msg:    "上传成功",
		Code:   "200",
		Result: key,
	})
}
